/**
 * Copyright (c) 2012, lincomb Technology CO,LDT. All rights reserved.
 * Module: 债权转让申请
 * File: DebtAssignmentAction.java
 * Purpose: 处理债权转让申请Action层
 * Date: 2016-5-9
 */
package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.DebtAssignmentService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;

/**
 * ClassName: DebtAssignmentAction 
 * @Description: 债权转让申请Action类
 * @author ZuoJun
 * @date 2016-5-9 10:50:29
 */
@Controller
@RequestMapping("/debt")

public class DebtAssignmentAction implements Serializable{

	private static final Log logger = LogFactory.getLog(DebtAssignmentAction.class);
	
	@Autowired
	private DebtAssignmentService debtAssignmentService;
	
	/**
	 * @Description: 处理债权转让申请
	 * @param request
	 * @param response
	 * @author ZuoJun
	 * @date 2016-5-9 11:49:39
	 */
	@RequestMapping(value = "/handleDebtAssignment")
	public void handleDebtAssignment(HttpServletRequest request, HttpServletResponse response){
				
		logger.info("---------------------------- /debt/requestDebtAssignment DebtAssignmentAction requestDebtAssignment() start ----------------------------");
		Map<String, Object> respMap = new HashMap<String, Object>();
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		//用户ID
		String userId = String.valueOf(paramsMap.get("userId")); 
		//订单ID
		String orderId = String.valueOf(paramsMap.get("orderId"));
		//客户种类(1:外部客户 2:内部客户)
		String customerType = String.valueOf(paramsMap.get("customerType"));
				
		//applyFlag：('Y'：申请 ; 'N'：取消)
		String applyFlag = String.valueOf(paramsMap.get("applyFlag"));
		
		logger.info("---------------------------- /debt/requestDebtAssignment DebtAssignmentAction requestDebtAssignment() userId=" 
				+ userId + " , orderId=" + orderId + " ,  customerType=" + customerType 
				+ " , applyFlag=" + applyFlag + " ----------------------------");
		
		if(StringUtils.isEmpty(applyFlag)){
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.CHECK_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/requestDebtAssignment DebtAssignmentAction requestDebtAssignment() applyFlag=" 
				+ applyFlag + " , applyFlag为空! ----------------------------");
			return;
		}
		if(StringUtils.isEmpty(userId)){
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.CHECK_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/requestDebtAssignment DebtAssignmentAction requestDebtAssignment() userId=" 
				+ userId + " , userId为空! ----------------------------");
			return;
		}
		if(StringUtils.isEmpty(orderId)){
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.CHECK_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/requestDebtAssignment DebtAssignmentAction requestDebtAssignment() orderId=" 
				+ orderId + " , orderId为空! ----------------------------");
			return;
		}
		
		if(StringUtils.isEmpty(customerType)){
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.CHECK_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/requestDebtAssignment DebtAssignmentAction requestDebtAssignment() customerType=" 
				+ customerType + " , customerType为空! ----------------------------");
			return;
		}
		//重新组装参数
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("orderId", orderId);
		params.put("customerType", customerType);
		params.put("applyFlag", applyFlag);
		
		try {
			respMap = debtAssignmentService.requestDebtAssignment(params);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.ERROR_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/requestDebtAssignment DebtAssignmentAction requestDebtAssignment() 出现异常 " 
				+ CommonUtil.printStackTraceToString(e) + " ----------------------------");
			return;
		}
				
		String result = CommonUtil.setResultStringCn(
				respMap,String.valueOf(respMap.get(Consts.RES_CODE)), 
			String.valueOf(respMap.get(Consts.RES_MSG)), String.valueOf(respMap.get(Consts.RES_MSG_CN)));
		
			CommonUtil.responseJson(result, response);
		
		logger.info("---------------------------- /debt/requestDebtAssignment DebtAssignmentAction requestDebtAssignment() end ----------------------------");
	}
	
	
	/**
	 * @Description: 债权转让利率列表
	 * @param request
	 * @param response
	 * @author ZuoJun
	 * @date 2016-5-20 15:36:39
	 */
	@RequestMapping(value = "/getDebtAssignmentRate")
	public void getDebtAssignmentRate(HttpServletRequest request, HttpServletResponse response){
				
		logger.info("---------------------------- /debt/getDebtAssignmentRate DebtAssignmentAction getDebtAssignmentRate() start ----------------------------");
		
		String result = "";
		Map<String, Object> respMap = new HashMap<String, Object>();
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		
		//起息日期
		String valueDate = paramsMap.get("valueDate"); 
		
		//客户种类(1:外部客户 2:内部客户)
		String customerType = paramsMap.get("customerType");
		//投资金额
		String investAmount = paramsMap.get("investAmount");
					
		logger.info("---------------------------- /debt/getDebtAssignmentRate DebtAssignmentAction getDebtAssignmentRate() investDate=" 
				+ valueDate + " ,  customerType=" + customerType + " , investAmount=" + investAmount + " ----------------------------");
		
		if(StringUtils.isEmpty(valueDate)){
					result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.CHECK_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/getDebtAssignmentRate DebtAssignmentAction getDebtAssignmentRate() investDate=" 
				+ valueDate + " , investDate为空! ----------------------------");
			return;
		}
		
		if(StringUtils.isEmpty(customerType)){
				result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.CHECK_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/getDebtAssignmentRate DebtAssignmentAction getDebtAssignmentRate() customerType=" 
				+ customerType + " , customerType为空! ----------------------------");
			return;
		}

		if(StringUtils.isEmpty(investAmount)){
				result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.CHECK_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/getDebtAssignmentRate DebtAssignmentAction getDebtAssignmentRate() investAmount=" 
				+ investAmount + " , investAmount为空! ----------------------------");
			return;
		}
		//重新组装参数
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("investDate", valueDate);
		params.put("userType", customerType);
		params.put("investAmount", investAmount);
		
		try {
			result = debtAssignmentService.getDebtAssignmentRate(params);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.ERROR_CODE, 
					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				logger.info("---------------------------- /debt/getDebtAssignmentRate DebtAssignmentAction getDebtAssignmentRate() 出现异常 " 
				+ CommonUtil.printStackTraceToString(e) + " ----------------------------");
			return;
		}
						
		CommonUtil.responseJson(result, response);
		
		logger.info("---------------------------- /debt/getDebtAssignmentRate DebtAssignmentAction getDebtAssignmentRate() end ----------------------------");
	}
	
	
}
